package com.xw.childhealth.controller;

import com.xw.childhealth.VO.DataDictionaryVO;
import com.xw.childhealth.VO.ResultVO;
import com.xw.childhealth.entity.Icd10Code;
import com.xw.childhealth.service.Icd10CodeService;
import com.xw.childhealth.util.ResultVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: y
 * @CreateDate: 2019-11-21 09:38
 * @Version: 1.0
 */
@RestController
@RequestMapping("/icd10Code")
@Slf4j
public class Icd10CodeController {

    @Autowired
    private Icd10CodeService icd10CodeService;


    @PostMapping("/getIcd10Code")
    public ResultVO getXqHospital() {
        return ResultVOUtil.success(recursionData(new ArrayList<>(), 0));
    }


    /**
     * 查找下级节点目录
     *
     * @param dataDictionaryVOList 数据字典 List
     * @param id                   查找的上级 id
     * @return
     */
    private List<DataDictionaryVO> recursionData(List<DataDictionaryVO> dataDictionaryVOList, Integer id) {

        List<Icd10Code> parentNode = icd10CodeService.findByParentIdEquals(id);
        if (parentNode == null || parentNode.isEmpty()) {
            return dataDictionaryVOList;
        }
        parentNode.forEach(e -> {
            DataDictionaryVO dataDictionaryVO = new DataDictionaryVO();
            dataDictionaryVO.setId(e.getId());
            dataDictionaryVO.setTitle(e.getInformation());

            if (e.getInformation() != null) {
                log.error(" ");
                List<DataDictionaryVO> list = recursionData(new ArrayList<>(), e.getId());

                dataDictionaryVO.setChildren(list.size() == 0 ? null : list);

                dataDictionaryVOList.add(dataDictionaryVO);
            }

        });

        return dataDictionaryVOList;
    }


    /**
     * 修改或者添加
     *
     * @param map
     * @return
     */
    @RequestMapping("/data-upd")
    public ModelAndView edit(@RequestParam(value = "type", required = false) String type,
                             @RequestParam(value = "nodeId", required = false) Integer nodeId,
                             @RequestParam(value = "icd10Code", required = false) String icd10Code,
                             @RequestParam(value = "name", required = false) String name,
                             @RequestParam(value = "pyCode", required = false) String pyCode,
                             @RequestParam(value = "numberCode", required = false) Integer numberCode,
                             @RequestParam(value = "zdyCode", required = false) String zdyCode,
                             @RequestParam(value = "sexRestriction", required = false) String sexRestriction,
                             @RequestParam(value = "addRecordNode", required = false) Integer addRecordNode, Map<String, Object> map) {
        if (type == null) {
            return new ModelAndView("/pages/admin/icd10code", map);
        }
        if (type.equals("toUpd")) {
            Icd10Code one = icd10CodeService.findByIdEquals(nodeId);
            map.put("type", type);
            map.put("result", one);
        } else if (type.equals("upd")) {
            Icd10Code one = icd10CodeService.findByIdEquals(nodeId);
            icd10CodeService.save(one.setIcd10Code(icd10Code).setName(name).setPyCode(pyCode).setNumberCode(numberCode).setZdyCode(zdyCode).setSexRestriction(sexRestriction).setUpdateTime(new Date()));
            return new ModelAndView("/pages/admin/icd10code", map);
        }
        if (type.equals("toAdd")) {
            map.put("addRecordNode", addRecordNode);
        } else if (type.equals("add")) {
            icd10CodeService.save(new Icd10Code().setIcd10Code(icd10Code).setName(name).setPyCode(pyCode).setNumberCode(numberCode).setZdyCode(zdyCode).setSexRestriction(sexRestriction).setUpdateTime(new Date()).setParentId(addRecordNode).setCreateTime(new Date()));
            return new ModelAndView("/pages/admin/icd10code", map);
        }
        return new ModelAndView("/pages/admin/icd10code-data-add", map);
    }


    /**
     * 查询父节点下面的子节点信息
     *
     * @param nodeId
     * @return
     */
    @PostMapping("/getNodeInfo/{nodeId}")
    public ResultVO getNodeInfo(@PathVariable Integer nodeId) {
        List<Icd10Code> result = icd10CodeService.findByParentIdEquals(nodeId);
        return ResultVOUtil.success(result);
    }

    @RequestMapping("/data-del")
    public ModelAndView del(@RequestParam("id") Integer id) {
        Icd10Code result = icd10CodeService.findByIdEquals(id);
        if (result != null) {
            icd10CodeService.delRecord(id);
        }
        return new ModelAndView("/pages/admin/icd10code");
    }


}
